import store from './../../../store';
import Vue from 'vue'

import user from '../../../store/modules/user.js'
import config from '../../../static/js/config/config.js'
const SystemInfo = uni.getSystemInfoSync();
//获取本地配置的服务器IP
let host_svr = config.host_svr
let wps_svr = config.wps_svr
let image_svr = config.image_svr


console.log(host_svr);
let uniReLaunchOnoff=true

async function doGet(url, req, successCallback, errorCallback){
	// console.log(host_svr);
	//检测网络状态
	// let Network=await getNetworkType()
	// console.log('网络状态',Network);
	return new Promise((resolve,reject)=>{
		//发送Get请求
		sendRequestGet(url,req).then(res=>{
			uni.setStorage({ key: 'networkCode', data: res.data.code});
			if(res.data.code=='400'||res.data.code=='700'){
				clearInterval(getApp().globalData.time)
				uni.reLaunch({
					url: '/pages/template/login/login',
					success: s => {
						setTimeout(()=>{
							uni.showToast({
								title: res.data.msg,
								icon:"none"
							});
						},500)
					}
				});
				return
			}
			resolve(res.data)
			successCallback&&successCallback(res.data)
		}).catch(error=>{
			reject(error)
			errorCallback&&errorCallback(error)
		})
	})
}
async function doPost(url, req, successCallback, errorCallback){
	//检测网络状态
	// let Network=await getNetworkType()
	// console.log('网络状态',Network);
	return new Promise((resolve,reject)=>{
		//发送Get请求
		sendRequestPost(url,req).then(res=>{
			if(res.data.code=='400'||res.data.code=='700'){
				clearInterval(getApp().globalData.time)
				uni.reLaunch({
					url: '/pages/template/login/login',
					success: s => {
						setTimeout(()=>{
							uni.showToast({
								title: res.data.msg,
								icon:"none"
							});
						},500)
					}
				});
				return
			}
			resolve(res.data)
			successCallback&&successCallback(res.data)
		}).catch(error=>{
			console.log(error);
			reject(error)
			errorCallback&&errorCallback(error)
		})
	})
}
//发送Get请求
async function sendRequestGet(url, req){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:host_svr + url,
			method: 'GET',
			data: req,
			header: {
				'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
				// 'content-type':'application/json',
				'Authorization': user.state.userToken
			},
			success: (res) => {
				resolve(res)
			},
			fail: (error) => {
				reject(error)
			}
		});
	})
}
//发送post请求
async function sendRequestPost(url, req){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:host_svr + url,
			method: 'POST',
			data: req,
			header: {
				'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
				// 'content-type':'application/json',
				'Authorization':user.state.userToken
			},
			success: (res) => {
				resolve(res)
			},
			fail: (error) => {
				reject(error)
			}
		});
	})
}
//文件上传
async function uploadFile(url, filePath,formData, successCallback, errorCallback){
	//检测网络状态
	// let Network=await getNetworkType()
	// console.log('网络状态',Network);
	return new Promise((resolve,reject)=>{
		//发送Get请求
		sendRequestploadFile(url,filePath,formData,function(uploadTask){
			successCallback&&successCallback(uploadTask)
		}).then(res=>{
			let data=JSON.parse(res)
			if(data.code=='400'||data.code=='700'){
				uni.reLaunch({
					url: '/pages/template/login/login',
					success: s => {
						setTimeout(()=>{
							uni.showToast({
								title:data.msg,
								icon:"none"
							});
						},500)
					}
				});
				return
			}
			resolve(data)
		}).catch(error=>{
			console.log(error);
			reject(error)
			errorCallback&&errorCallback(error)
		})
	})
}
//发送文件上传请求
function sendRequestploadFile(url,filePath,formData,callback){
	return new Promise((resolve,reject)=>{
		var uploadTask = uni.uploadFile({
			url: host_svr + url,
			filePath: filePath,
			name: 'file',
			header: {
				'Authorization':user.state.userToken
			},
			formData:formData,//其他参数
			success: (uploadFileRes) => {
				console.log(uploadFileRes);
				resolve(uploadFileRes.data)
			},
			fail(error) {
				console.log(error);
				reject(error)
			}
		});
		callback(uploadTask)
	})
}
//检测网络状态
function getNetworkType(){
	return new Promise((resolve,reject)=>{
		uni.getNetworkType({
			success: (res) => {
				if(res.networkType=="none"){
					uni.showModal({
						title: '提示',
						content: '当前无网络，请检查网络状态',
						showCancel:false,
						success: function (res) {}
					});
				}
				resolve(0)
			},
		    fail: () => {
				uni.showModal({
					title: '提示',
					content: '当前无网络，请检查网络状态',
					showCancel:false,
					success: function (res) {}
				});
				reject(1)
		    }
		})
	})
}




module.exports = {
	host_svr,
	image_svr,
	wps_svr,
	doGet,
	doPost,
	uploadFile,//文件上传
}
